<?php

class BaseController extends Controller {

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}

	protected function serializeObject($obj) {
        return json_encode($obj);
    }

	protected function makeResponse($code, $array = null, $data = null) {
        $result = array("Result" => $code);
        if(isset($array) && isset($data))
            array_set($result, $array, $data);
        // if ($code != 200) {
        //     App::abort(200, $this->serializeObject($result));
        // }
        $response = Response::make($this->serializeObject($result));
        $response->header("Content-Type", "application/json");
        return $response;
    }

	protected function makeJson($code, $data=null) {
        $result = array("Result" => $code);
        if (!is_null($data)) array_set($result, "Records", $data);
        // if ($code != 200) {
        //     App::abort(200, $this->serializeObject($result));
        // }
        $response = Response::make($this->serializeObject($result));
        $response->header("Content-Type", "application/json");
        return $response;
    }

    protected function makeOption($code, $data=null) {
        $result = array("Result" => $code);
        if (!is_null($data)) array_set($result, "Options", $data);
        // if ($code != 200) {
        //     App::abort(200, $this->serializeObject($result));
        // }
        $response = Response::make($this->serializeObject($result));
        $response->header("Content-Type", "application/json");
        return $response;
    }    

    public function checkauthed()
    {
        if(Auth::check()){
            return Redirect::to('/index');
        }else{
            return Redirect::to('/login');
        }
    }
}
